package com.honglv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.honglv.context.BaseContext;
import com.honglv.mapper.CommodityMapper;
import com.honglv.mapper.HotelMapper;

import com.honglv.pojo.dto.*;
import com.honglv.pojo.entity.Commodities;
import com.honglv.pojo.entity.Hotel;
import com.honglv.pojo.entity.Hotel;
import com.honglv.pojo.vo.*;
import com.honglv.pojo.vo.GetHotelsByCityVO;
import com.honglv.pojo.result.Result;
import com.honglv.service.CommodityService;
import com.honglv.service.HotelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class HotelServiceImpl implements HotelService {
    @Autowired
    private HotelMapper hotelMapper;
    /**
     * 管理员上传酒店
     * @param
     * @return
     */
    @Override
    public void upload(UploadHotelDTO uploadHotelDTO) {
        Long authorId = BaseContext.getCurrentId();
        Hotel hotel=new Hotel();
        BeanUtils.copyProperties(uploadHotelDTO,hotel);
        hotel.setAuthorId(authorId);
        hotelMapper.insert(hotel);


    }
    /**
     * 根据城市分页获取酒店
     * @param pageByCityDTO
     * @return
     */
    @Override
    public PageVO<GetHotelsByCityVO> getHotelsByCity(PageByCityDTO pageByCityDTO) {
        PageHelper.startPage(pageByCityDTO.getPage(),pageByCityDTO.getPageSize());
        QueryWrapper<Hotel> qw=new QueryWrapper<>();
        qw.like("city",pageByCityDTO.getCity());
        List<Hotel> list = hotelMapper.selectList(qw);
        GetHotelsByCityVO gf=new GetHotelsByCityVO();
        List<GetHotelsByCityVO> hotels=new ArrayList<>();
        for(Hotel Hotel:list){
            BeanUtils.copyProperties(Hotel,gf);
            hotels.add(gf);
        }
        if (hotels == null){
            return null;
        }
        PageInfo<GetHotelsByCityVO> pageInfo = new PageInfo<>(hotels);
        return new PageVO<>(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * Description:通过用户id获取酒店
     * date: 2024/04/01
     * author:王铭杰
     */
    @Override
    public List<GetAllHotelVO> getHotelsByAuthorId(Long userId) {
        List<GetAllHotelVO> list = hotelMapper.getHotelsByAuthorId(userId);
        return list;
    }

    @Override
    public Result deleteHotel(Long id) {
        hotelMapper.deleteHotel(id);
        return Result.success("删除成功！");
    }

    @Override
    public Result updateHotel(UpdateHotelDTO updateHotelDTO) {
        if(hotelMapper.updateHotel(updateHotelDTO) > 0){
            return Result.success("修改成功！");
        }
        return Result.error("修改失败，未知错误！");
    }
}

